<template>
  <div>
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">{{ $t('m.common.button.add') }}</a-button>
      <a-button @click="batchDel" type="default"  icon="delete">{{ $t('m.common.button.delete') }}</a-button>

      <div style="float: right">
        <a-input-search style="width: 200px;" :placeholder="$t('m.common.label.guan-jian-zi')" enter-button @search="onSearch"></a-input-search>
      </div>
    </div>

    <!-- table区域-begin -->
    <a-table
      ref="table"
      size="middle"
      bordered
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      @change="handleTableChange">
      <template slot="serialNum" slot-scope="text,record,index">
        {{(ipagination.current-1) * ipagination.pageSize + parseInt(index) + 1}}
      </template>
      <template slot="action" slot-scope="text, record">
        <a @click="handleEdit(record)">{{$t('m.bpm.other.process_design')}}</a>
        <a-divider type="vertical"/>
        <a @click="handleStartRolePage(record.id)">{{$t('m.bpm.other.start_permission')}}</a>
        <a-divider type="vertical"/>
        <a-dropdown>
          <a class="ant-dropdown-link">{{$t('m.common.button.more')}}
            <ych-icon type="down"/>
          </a>
          <a-menu slot="overlay">
            <a-menu-item>
              <a @click="handleNewVersion(record)">{{$t('m.bpm.other.new_version')}}</a>
            </a-menu-item>
            <a-menu-item>
              <a @click="handleGetUrl(record)">{{$t('m.bpm.other.start_url')}}</a>
            </a-menu-item>
            <a-menu-item>
              <a-popconfirm :title="$t('m.bpm.verify.confirm_delete_template')" @confirm="() => handleDelete(record.id)">
                <a>{{ $t('m.common.button.delete') }}</a>
              </a-popconfirm>
            </a-menu-item>
            <a-menu-item v-if="record.design">
              <a-popconfirm :title="$t('m.bpm.verify.confirm_publish_template')" @confirm="() => handleActivate(record)">
                <a>{{$t('m.bpm.other.publish_template')}}</a>
              </a-popconfirm>
            </a-menu-item>
            <a-menu-item v-if="record.suspended">
              <a-popconfirm :title="$t('m.common.tip.enable_confirm')" @confirm="() => handleActivate(record)">
                <a>{{$t('m.bpm.other.enable_template')}}</a>
              </a-popconfirm>
            </a-menu-item>
            <a-menu-item v-if="!record.suspended && !record.design">
              <a-popconfirm :title="$t('m.common.tip.disable_confirm')" @confirm="() => handleSuspend(record)">
                <a>{{$t('m.bpm.other.disable_template')}}</a>
              </a-popconfirm>
            </a-menu-item>
          </a-menu>
        </a-dropdown>
      </template>

    </a-table>

    <bpm-designer-dialog ref="bpmDesignerDialog" @ok="modalFormOk" :groupId="groupId"/>

    <bpm-start-role-dialog ref="startRole"/>
  </div>
</template>

<script>

  import { YcListMixin } from '@/mixins/YcListMixin'
  import { putAction } from '@/api/manage'
  import { getFlowUrl } from '@/utils/hbdUtil'
  import { filterMultiDictText } from '@/components/dict/YchDictSelectUtil'
  import BpmDesignerDialog from './modules/BpmDesignerDialog'
  import BpmStartRoleDialog from './modules/BpmStartRoleDialog'
  import { BpmConstant } from './BpmConstant'
  import YchConstant from "@/api/YchConstant";

  export default {
    name: 'ProcessDefList',
    mixins: [YcListMixin],
    components: {
      BpmDesignerDialog,
      BpmStartRoleDialog,
    },
    props: {
      groupId: {
        type: String,
        required: true
      },
    },
    data () {
      return {
        delTip: this.$t('m.bpm.verify.delete_tip'),
        queryParam: {
          catalogId: this.groupId
        },
        columns: [{
          title:this.$t('m.common.label.number'),
          dataIndex: '',
          key: 'rowIndex',
          width: 50,
          align: 'center',
          scopedSlots: { customRender: 'serialNum' },
        }, {
          title: this.$t('m.bpm.entity.name'),
          align: 'center',
          dataIndex: 'name'
        }, {
          title: this.$t('m.bpm.entity.key'),
          align: 'center',
          dataIndex: 'key'
        }, {
          title: this.$t('m.bpm.entity.version'),
          align: 'center',
          dataIndex: 'version'
        }, {
          title: this.$t('m.bpm.entity.status'),
          align: 'center',
          dataIndex: 'status',
          customRender: (text) => {
            if (!text) {
              return ''
            } else {
              return filterMultiDictText(BpmConstant.processDefStatus, text)
            }
          }
        }, {
          title: this.$t('m.common.label.operate'),
          dataIndex: 'action',
          align: 'center',
          width: 320,
          scopedSlots: { customRender: 'action' },
        }],
        url: {
          list: YchConstant.PREFIX_BPM + '/bpm-back/processDefRest/list/v1',
          delete: YchConstant.PREFIX_BPM + '/bpm-back/processDefRest/delete/v1',
          deleteBatch: YchConstant.PREFIX_BPM + '/bpm-back/processDefRest/deleteBatch/v1',
          suspend: YchConstant.PREFIX_BPM + '/bpm-back/processDefRest/suspend/v1',
          activate: YchConstant.PREFIX_BPM + '/bpm-back/processDefRest/activate/v1',
        }
      }
    },
    methods: {
      handleAdd: function () {
        this.$refs.bpmDesignerDialog.add()
      },
      handleNewVersion (record) {
        this.$refs.bpmDesignerDialog.edit(record, true)
        this.$refs.bpmDesignerDialog.type = 'newVersion'
      },
      handleEdit: function (record) {
        this.$refs.bpmDesignerDialog.edit(record, record.design)
      },
      handleSuspend (record) {
        const that = this
        putAction(this.url.suspend + '?id=' + record.id, {}).then((res) => {
          if (res.success) {
            that.messageUtil.success(res.message)
            that.loadData()
          } else {
            that.messageUtil.warning(res.message)
          }
        }).finally(() => {
        })
      },
      handleActivate (record) {
        const that = this
        putAction(this.url.activate + '?id=' + record.id, {}).then((res) => {
          if (res.success) {
            that.messageUtil.success(res.message)
            that.loadData()
          } else {
            that.messageUtil.warning(res.message)
          }
        }).finally(() => {
        })
      },
      handleStartRolePage (id) {
        this.$refs.startRole.open(id)
      },
      handleGetUrl (record) {
        const url = getFlowUrl(record.id)
        this.$info({
          title: this.$t('m.bpm.other.start_url'),
          content: url
        })
      }
    }
  }
</script>
<style lang="less" scoped>

</style>
